package com.qkl.xiatang.app.service.impl;

import com.qkl.xiatang.app.dto.task.TaskTimeInDTO;
import com.qkl.xiatang.app.dto.task.TaskTimeVO;
import com.qkl.xiatang.app.entity.feed.AllocBaseEntity;
import com.qkl.xiatang.app.entity.feed.FeedAllocDetailEntity;
import com.qkl.xiatang.app.mapper.TaskTimeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class TaskTimeService {

    @Autowired
    private TaskTimeMapper taskTimeMapper;

    public TaskTimeVO queryTaskTime(String companyId, String tableName, String timeFieldName, Date allocDate,Integer serialNo,String feedId){
        TaskTimeInDTO params = new TaskTimeInDTO();
        params.setSerialNo(serialNo);
        params.setAllocDate(allocDate);
        params.setTableName(tableName);
        params.setTimeFieldName(timeFieldName);
        params.setCompanyId(companyId);
        //params.setFeedId(feedId);
        return taskTimeMapper.queryTaskTime(params);
    }

    public TaskTimeVO queryFeedTaskTime(String companyId, String timeFieldName, Date allocDate,Integer serialNo,String feedId){
        return this.queryTaskTime(companyId,"feed_alloc_detail",timeFieldName,allocDate,serialNo,feedId);
    }

    public TaskTimeVO queryFeedTaskTime(AllocBaseEntity task){
        if(task instanceof FeedAllocDetailEntity){
            FeedAllocDetailEntity feedTask = (FeedAllocDetailEntity)task;
            return this.queryFeedFeedTaskTime(task.getCompanyId(), task.getAllocDate(), feedTask.getSerialNo(),task.getFeedId());
        }
        return this.queryZooFeedTaskTime(task.getCompanyId(),task.getAllocDate(),task.getFeedId());
    }

    public TaskTimeVO queryWeighTaskTime(AllocBaseEntity task){
        if(task instanceof FeedAllocDetailEntity){
            FeedAllocDetailEntity feedTask = (FeedAllocDetailEntity)task;
            return this.queryFeedWeighTaskTime(task.getCompanyId(), task.getAllocDate(), feedTask.getSerialNo(),task.getFeedId());
        }
        return this.queryZooWeighTaskTime(task.getCompanyId(),task.getAllocDate(),task.getFeedId());
    }

    public TaskTimeVO queryFeedFeedTaskTime(String companyId, Date allocDate,Integer serialNo,String feedId){
        return this.queryFeedTaskTime(companyId,"feed_time",allocDate,serialNo,feedId);
    }

    public TaskTimeVO queryFeedWeighTaskTime(String companyId, Date allocDate,Integer serialNo,String feedId){
        return this.queryFeedTaskTime(companyId,"weigh_time",allocDate,serialNo,feedId);
    }

    public TaskTimeVO queryFeedCheckTaskTime(String companyId, Date allocDate,Integer serialNo,String feedId){
        return this.queryFeedTaskTime(companyId,"check_time",allocDate,serialNo,feedId);
    }

    public TaskTimeVO queryZooTaskTime(String companyId, String timeFieldName, Date allocDate,String feedId){
        return this.queryTaskTime(companyId,"zoo_alloc_detail",timeFieldName,allocDate,null,feedId);
    }

    public TaskTimeVO queryZooWeighTaskTime(String companyId, Date allocDate,String feedId){
        return this.queryZooTaskTime(companyId,"weigh_time",allocDate,feedId);
    }

    public TaskTimeVO queryZooFeedTaskTime(String companyId, Date allocDate,String feedId){
        return this.queryZooTaskTime(companyId,"feed_time",allocDate,feedId);
    }

}
